package com.stx.jeepay.manager.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.stx.jeepay.core.aop.MethodLog;
import com.stx.jeepay.core.entity.SysConfigEntity;
import com.stx.jeepay.core.enums.ApiCodeEnum;
import com.stx.jeepay.core.model.ApiRes;
import com.stx.jeepay.core.mq.model.ResetAppConfigMQ;
import com.stx.jeepay.core.mq.vender.IMQSender;
import com.stx.jeepay.core.service.SysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Api(tags = "系统管理（配置信息类）")
@Slf4j
@RestController
@RequestMapping("api/sysConfigs")
public class SysConfigController extends CommonCtrl {
    @Autowired
    private SysConfigService sysConfigService;
    @Autowired
    private IMQSender mqSender;

    @ApiOperation("系统配置--查询分组下的配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iToken", value = "用户身份凭证", required = true, paramType = "header"),
            @ApiImplicitParam(name = "groupKey", value = "分组key")
    })
    @PreAuthorize("hasAuthority('ENT_SYS_CONFIG_INFO')")
    @GetMapping("/{groupKey}")
    public ApiRes<List<SysConfigEntity>> getConfigs(@PathVariable("groupKey") String groupKey){
        LambdaQueryWrapper<SysConfigEntity> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(groupKey)){
            queryWrapper.eq(SysConfigEntity::getGroupKey,groupKey);
        }
        queryWrapper.orderByDesc(SysConfigEntity::getSortNum);
        List<SysConfigEntity> list = sysConfigService.list(queryWrapper);
        return ApiRes.ok(list);
    }

    @ApiOperation("系统配置--修改分组下的配置")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iToken", value = "用户身份凭证", required = true, paramType = "header"),
            @ApiImplicitParam(name = "groupKey", value = "分组key", required = true),
            @ApiImplicitParam(name = "mchSiteUrl", value = "商户平台网址(不包含结尾/)"),
            @ApiImplicitParam(name = "mgrSiteUrl", value = "运营平台网址(不包含结尾/)"),
            @ApiImplicitParam(name = "ossPublicSiteUrl", value = "公共oss访问地址(不包含结尾/)"),
            @ApiImplicitParam(name = "paySiteUrl", value = "支付网关地址(不包含结尾/)")
    })
    @PreAuthorize("hasAuthority('ENT_SYS_CONFIG_EDIT')")
    @MethodLog(remark = "系统配置修改")
    @PutMapping("/{groupKey}")
    public ApiRes update(@PathVariable("groupKey") String groupKey) {
        JSONObject paramJSON = getReqParamJSON();
        Map<String, String> updateMap = JSONObject.toJavaObject(paramJSON, Map.class);
        int update = sysConfigService.updateByConfigKey(updateMap);
        if (update<=0){
            return ApiRes.fail(ApiCodeEnum.SYSTEM_ERROR, "更新失败");
        }

        // 异步更新到MQ
        this.updateSysConfigMQ(groupKey);

        return ApiRes.ok();
    }

    @Async
    public void updateSysConfigMQ(String groupKey){
        mqSender.send(ResetAppConfigMQ.build(groupKey));
    }
}
